//
//  CurentRoomController.m
//  Home
//
//  Created by 新宝 on 2017/8/8.
//  Copyright © 2017年 新宝. All rights reserved.
//

#import "CurrentRoomController.h"
#import "SearchAccessoryController.h"
@interface CurrentRoomController ()
@property(nonatomic,strong)UIButton *button;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UIScrollView *scollView;
@property(nonatomic,strong)MyHomeKit *homeKit;
@property(nonatomic,strong)NSArray *roomArry;
@property (nonatomic,strong)HMRoom *currentRoom;

@end

@implementation CurrentRoomController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    
  }




- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    NSString *str=[NSString stringWithFormat:@"Home：%@",self.currentHome.name];
    self.navigationItem.title = str;
    self.button=[[UIButton alloc]initWithFrame:CGRectMake(0, 100, 80, 30)];
    [self.button setTitle:@"创建房间" forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(creatRoomButton) forControlEvents: UIControlEventTouchDown];
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithCustomView:self.button];
    self.navigationItem.rightBarButtonItem=rightItem;
    
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.view addSubview:self.tableView];
  
    self.roomArry=self.currentHome.rooms;
    [self.tableView reloadData];
}



-(void)creatRoomButton
{
    UIAlertController *inputNameAlert=[UIAlertController alertControllerWithTitle:@"请输入新room的名字" message:@"请确保这个名字的唯一性" preferredStyle:UIAlertControllerStyleAlert];
    __weak typeof (self) weakSelf = self;
    
    [inputNameAlert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder=@"请输入新房间的名字";
    }];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:0 handler:^(UIAlertAction * _Nonnull action) {
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:0 handler:^(UIAlertAction * _Nonnull action) {
    NSString *newName =inputNameAlert.textFields.firstObject.text;
        
        [self.currentHome addRoomWithName:newName completionHandler:^(HMRoom * _Nullable room, NSError * _Nullable error) {
            if(!error)
            {
                weakSelf.roomArry=weakSelf.currentHome.rooms;
                [weakSelf.tableView reloadData];
                
            }
            
        }];
        
        
    }];
    [inputNameAlert addAction:action1];
    [inputNameAlert addAction:action2];
    [self presentViewController:inputNameAlert animated:YES completion:^{}];
    
}

#pragma mark UITableView代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.roomArry.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[[UITableViewCell alloc]init];
    HMRoom *room=self.roomArry[indexPath.row];
    cell.textLabel.text=room.name;
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SearchAccessoryController *accessoryVC=[[SearchAccessoryController alloc]init];
    HMRoom *room=self.roomArry[indexPath.row];
    accessoryVC.currentRoom=room;
    accessoryVC.currentHome=self.currentHome;
    [self.navigationController pushViewController:accessoryVC animated:YES];
   
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
    
}


@end
